package com.example.robertluozizhao.framecollectdemo.view.custom;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by Lzz on 8/3/2019.
 */

public class InternalInteceptRecycleView extends RecyclerView {

    private float mLastX;
    private float mLastY;

    public InternalInteceptRecycleView(Context context) {
        this(context, null);
    }

    public InternalInteceptRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //请求父容器不拦截后续系列事件，
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = ev.getX();
                float moveY = ev.getY();
                if (Math.abs(moveX- mLastX)>Math.abs(moveY- mLastY)) {
                    //请父类容器不拦截事件为false,即父容器会拦截之后系列事件
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        mLastX = ev.getX();
        mLastY = ev.getY();
        return super.dispatchTouchEvent(ev);
    }
}
